package org.example.src.service.impl;

import org.example.src.dto.request.FinalReportRequest;
import org.example.src.dto.response.FinalReportResponse;
import org.example.src.entity.Application;
import org.example.src.entity.FinalReport;
import org.example.src.entity.Student;
import org.example.src.repository.ApplicationRepository;
import org.example.src.repository.FinalReportRepository;
import org.example.src.repository.StudentRepository;
import org.example.src.service.FinalReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class FinalReportServiceImpl implements FinalReportService {

    @Autowired
    private FinalReportRepository finalReportRepository;

    @Autowired
    private StudentRepository studentRepository;

    @Autowired
    private ApplicationRepository applicationRepository;

    @Override
    public List<FinalReport> findAllFinalReports() {
        return finalReportRepository.findAll();
    }

    @Override
    public FinalReport findFinalReportById(Long id) {
        return finalReportRepository.findById(id).orElse(null);
    }

    @Override
    public FinalReport createFinalReport(FinalReportRequest request) {
        Student student = studentRepository.findById(request.getStudentId())
                .orElseThrow(() -> new RuntimeException("学生未找到"));

        FinalReport finalReport = new FinalReport();
        finalReport.setTitle(request.getTitle());
        finalReport.setContent(request.getContent());
        finalReport.setSubmissionDate(request.getSubmissionDate());
        finalReport.setStudent(student);

        if (request.getApplicationId() != null) {
            Application application = applicationRepository.findById(request.getApplicationId())
                    .orElseThrow(() -> new RuntimeException("申请未找到"));
            finalReport.setApplication(application);
        }

        return finalReportRepository.save(finalReport);
    }

    @Override
    public FinalReport updateFinalReport(Long id, FinalReportRequest request) {
        FinalReport finalReport = finalReportRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("最终报告未找到"));

        finalReport.setTitle(request.getTitle());
        finalReport.setContent(request.getContent());
        finalReport.setSubmissionDate(request.getSubmissionDate());
        finalReport.setTeacherFeedback(request.getTeacherFeedback());
        finalReport.setCompanyFeedback(request.getCompanyFeedback());
        finalReport.setScore(request.getScore());

        if (request.getApplicationId() != null) {
            Application application = applicationRepository.findById(request.getApplicationId())
                    .orElseThrow(() -> new RuntimeException("申请未找到"));
            finalReport.setApplication(application);
        }

        return finalReportRepository.save(finalReport);
    }

    @Override
    public void deleteFinalReport(Long id) {
        finalReportRepository.deleteById(id);
    }

    @Override
    public List<FinalReport> findFinalReportsByStudentId(Long studentId) {
        return finalReportRepository.findByStudentId(studentId);
    }

    @Override
    public List<FinalReport> findFinalReportsByApplicationId(Long applicationId) {
        return finalReportRepository.findByApplicationId(applicationId);
    }

    @Override
    public List<FinalReportResponse> getFinalReportsByStudentId(Long studentId) {
        List<FinalReport> finalReports = finalReportRepository.findByStudentId(studentId);
        return finalReports.stream().map(this::convertToResponse).collect(Collectors.toList());
    }

    private FinalReportResponse convertToResponse(FinalReport finalReport) {
        FinalReportResponse response = new FinalReportResponse();
        BeanUtils.copyProperties(finalReport, response);

        if (finalReport.getStudent() != null) {
            response.setStudentId(finalReport.getStudent().getId());
            response.setStudentName(finalReport.getStudent().getFullName());
        }

        if (finalReport.getApplication() != null) {
            response.setApplicationId(finalReport.getApplication().getId());
        }

        return response;
    }
}
